package pers.qianyu.month_202101.date_20210113;

import java.util.*;

/**
 * 341. 扁平化嵌套列表迭代器
 * https://leetcode-cn.com/problems/flatten-nested-list-iterator/
 *
 * @author mizzle rain
 * @date 2021年1月13日20:57:52
 */
public class NestedIterator implements Iterator<Integer> {
    private List<Integer> seq = new ArrayList<>();
    private int cur = 0;

    public NestedIterator(List<NestedInteger> nestedList) {
        dfs(nestedList);
    }

    private void dfs(List<NestedInteger> nestedList) {
        for (NestedInteger n : nestedList) {
            if (n.isInteger()) {
                seq.add(n.getInteger());
            } else {
                dfs(n.getList());
            }
        }
    }

    @Override
    public Integer next() {
        return seq.get(cur++);
    }

    @Override
    public boolean hasNext() {
        return cur < seq.size();
    }
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */